package db.exchange.client.clientgui;

import db.exchange.client.Client;
import db.exchange.client.ClientStatus;
import db.exchange.client.IClientController;
import db.exchange.message.Operation;
import db.exchange.message.Order;

public class ControllerGUI implements IClientController {

	Client client;
	MainWindow window;

//	public String validate(String symbol, String qty, String Price,
//			boolean isMarket) {
//
//		return "ok";
//	}

	public String constructNewOrder(String clientId, String orderId,
			String symbol, String operationString, String priceString,
			String qtyString)  {
		double price = 0;
		Operation operation = Operation.BUY;
		int qty = 0;
		String errorMessage = "Order is sent";
		operation = ("BUY".equals(operationString)) ? Operation.BUY
				: Operation.SELL;
		try {
			qty = Integer.parseInt(qtyString);
		} catch (NumberFormatException e) {
			return "qty is wrong";
		}
		try {
			price = Double.parseDouble(priceString);
		} catch (NumberFormatException e) {
			return "price is wrong";
		}
		orderId = client.getNewOrderId();
		Order order = new Order(clientId, orderId, symbol, operation, price, qty);
		client.putNewOrder(order);
		return errorMessage;
	}

	// public void refresh() {
	// client.getOrders().keySet();
	// StringBuilder row = new StringBuilder();
	// ArrayList<String> keys = new
	// ArrayList<String>(client.getOrders().keySet());
	// Collections.sort(keys);
	// Collections.reverse(keys);
	// for (String key : keys) {
	// row.append(client.getOrders().get(key));
	// row.append("\n");
	// }
	// window.textPane_allOrders.setText(row.toString());
	//
	// }

	public void updateTip(String tip) {
		window.lblHelp.setText(tip);
	}

	@Override
	public void updateStatus(ClientStatus status, String message) {
		window.lblHelp.setText(message);
		switch (status) {
		case DISCONNECT:
			// window.
			break;
		case CONNECT:

			break;
		case LOGIN:

			break;
		default:
			break;
		}

	}

	@Override
	public void updateStatusClients(String allOrdersAsString) {
		window.textPane_allOrders.setText(allOrdersAsString);

	}

}
